package com.sws.common.utils.access_tokenutil.controller;


import com.sws.common.utils.access_tokenutil.model.DeviceToken;
import com.sws.common.utils.access_tokenutil.service.IAccessTokenService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 访问令牌 控制器
 * 
 * @author sws
 */
@Api(tags = {"访问令牌 控制器"})
@RestController
@RequestMapping("/token")
public class AccessTokenController {

    @Autowired
    private IAccessTokenService accessTokenService;

    /**
     * 获取设备访问令牌
     */
    @ApiOperation("获取设备访问令牌")
    @GetMapping("/get/{deviceIp}")
    public ResponseEntity<?> getAccessToken(@PathVariable String deviceIp) {
        DeviceToken deviceToken = accessTokenService.getAccessToken(deviceIp);
        if (deviceToken != null) {
            return ResponseEntity.ok(deviceToken);
        } else {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 500);
            response.put("message", "获取设备访问令牌失败");
            return ResponseEntity.status(500).body(response);
        }
    }

    /**
     * 刷新设备访问令牌
     */
    @ApiOperation("刷新设备访问令牌")
    @PostMapping("/refresh/{deviceIp}")
    public ResponseEntity<?> refreshAccessToken(@PathVariable String deviceIp) {
        boolean result = accessTokenService.refreshAccessToken(deviceIp);
        if (result) {
            DeviceToken deviceToken = accessTokenService.getAccessToken(deviceIp);
            return ResponseEntity.ok(deviceToken);
        } else {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 500);
            response.put("message", "刷新设备访问令牌失败");
            return ResponseEntity.status(500).body(response);
        }
    }

    /**
     * 直接获取访问令牌字符串
     */
    @ApiOperation("直接获取访问令牌字符串")
    @GetMapping("/string/{deviceIp}")
    public ResponseEntity<?> getToken(@PathVariable String deviceIp) {
        String token = accessTokenService.getToken(deviceIp);
        if (token != null) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("message", "获取访问令牌成功");
            response.put("token", token);
            return ResponseEntity.ok(response);
        } else {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 500);
            response.put("message", "获取访问令牌失败");
            return ResponseEntity.status(500).body(response);
        }
    }


    /**
     * 刷新所有设备的访问令牌
     */
    @ApiOperation("刷新所有设备的访问令牌")
    @PostMapping("/refresh/all")
    public ResponseEntity<?> refreshAllAccessTokens() {
        accessTokenService.refreshAllAccessTokens();
        Map<String, Object> response = new HashMap<>();
        response.put("code", 200);
        response.put("message", "已触发刷新所有设备的访问令牌");
        return ResponseEntity.ok(response);
    }
} 